package client;

import msg.MsgHistoryBack;
import java.util.ArrayList;

/**
 *
 * @author tomek
 */
public class HistoryDisplay {
    
    MsgHistoryBack history = new MsgHistoryBack();
    gui.HistoryDisplayGui myGui;
    
    
    public HistoryDisplay(MsgHistoryBack msg) {
        history = msg;
        myGui = new gui.HistoryDisplayGui(sortHistory());
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                myGui.showGui();
            }
        });
    }
    
    public ArrayList<Talk> sortHistory() {
        ArrayList<Talk> talkList = new ArrayList<Talk>();
        
        for (int i = 0; i < history.getMessage().size(); i++) {
            String actualSender = history.getSenders().get(i);
            String actualReceiver = history.getReceivers().get(i);
            String actualDate = history.getDate().get(i).toString();
            String actualMsg = history.getMessage().get(i);
            
            if(talkList.size() > 0) {
                for (int j = 0; j < talkList.size(); j++) {
                    //sprawdza czy dana para rozmówców ma juz swoj wpis w liscie
                    if((actualSender.equalsIgnoreCase(talkList.get(j).person1) && actualReceiver.equalsIgnoreCase(talkList.get(j).person2)) ||
                       (actualSender.equalsIgnoreCase(talkList.get(j).person2) && actualReceiver.equalsIgnoreCase(talkList.get(j).person1))) {
                       talkList.get(j).addMessage("["+actualDate+"]"+"<"+actualSender+"> "+actualMsg);
                    }
                    else if(j == talkList.size()-1) {
                        talkList.add(new Talk(actualSender, actualReceiver, "["+actualDate+"]"+"<"+actualSender+"> "+actualMsg));
                    }
                    
                }
                
            }
            else {
                talkList.add(new Talk(actualSender, actualReceiver, "["+actualDate+"]"+"<"+actualSender+"> "+actualMsg));
            }
        }
        return talkList;
    }
    

    
    public static void main(String[] args) {

    }
    
}
